package com.aplink.generic.pojo.formdesign;

import java.util.ArrayList;
import java.util.Locale;

import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.text.TextUtils;

import com.aplink.generic.GenericApplication;

public class SelectWhere extends ArrayList<Column> {
	private static final long serialVersionUID = 6037436530253459416L;
	private String mTargetClass;

	public SelectWhere() {

	}

	private ActivityInfo getActivityInfo(final String activityName)
	        throws NameNotFoundException {
		final String packageName = GenericApplication.getInstance()
		        .getApplicationContext().getPackageName();
		final ActivityInfo[] listActivity = GenericApplication.getInstance()
		        .getApplicationContext().getPackageManager()
		        .getPackageInfo(packageName, PackageManager.GET_ACTIVITIES).activities;
		for (final ActivityInfo activityInfo : listActivity) {
			if (activityInfo.name.toLowerCase(Locale.getDefault()).contains(
			        activityName.toLowerCase(Locale.getDefault()))) {
				return activityInfo;
			}
		}
		return null;
	}

	public Class<?> getTargetClass() throws NameNotFoundException,
	        ClassNotFoundException {
		if (TextUtils.isEmpty(this.mTargetClass)) {
			return null;
		}
		final ActivityInfo activityInfo = getActivityInfo(this.mTargetClass);
		final Class<?> activityClass = Class.forName(activityInfo.name);
		return activityClass;
	}

	public void setTargetClass(final String targetClass) {
		this.mTargetClass = targetClass;
	}
}
